package com.lgx.group.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lgx.common.web.Constants;
import com.lgx.common.web.Pagination;
import com.lgx.mobile.service.IGroupService;
import com.lgx.mobile.vo.GroupVO;
import com.lgx.right.entity.User;

@Controller
@RequestMapping("/group")  
public class GroupController {
	@Autowired
	IGroupService groupService;
	
	public IGroupService getGroupService() {
		return groupService;
	}

	@RequestMapping("/group.ftl")
	public String list(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		
		request.setAttribute("basePath", basePath);
		
		//热门圈子列表
		int num = 7;
		List<GroupVO> hotGroupList = this.groupService.findHotGroupList(num);
		request.setAttribute("hotGroupList", hotGroupList);
		
		request.setAttribute("hotGroupTop1", hotGroupList != null && hotGroupList.size() != 0?hotGroupList.get(0):null);
		
		//我的圈子列表
//		List<Group> groupList = this.groupService.findGroupList();
//		request.setAttribute("groupList", groupList);
		String pageNum = request.getParameter("page");
		String pageSize= request.getParameter("rows");
		
		if(StringUtils.isEmpty(pageSize))pageSize = "6";
		if(StringUtils.isEmpty(pageNum))pageNum = Constants.FIRST_PAGE_NUM;
		
		Object[] params = new Object[10];
		
		long userId = 0;
		User onlineUser = (User) request.getSession().getAttribute(Constants.LOGIN_USER);//得到当前登录人信息
		if(onlineUser != null){
			userId = onlineUser.getId();
		}
		
		Pagination myGroupPage = this.groupService.findMyGroupPagination(userId, params, GroupVO.class, Integer.parseInt(pageSize), Integer.parseInt(pageNum));
		request.setAttribute("myGroupPage", myGroupPage);
		
		request.setAttribute("pageNum", pageNum);
		
		return "group/group";
	}
	
	@RequestMapping("/index.htm")
	public String index(HttpServletRequest request,HttpServletResponse response){
		return "group/group";
	}

	
	@RequestMapping("/groupSearch.htm")  
	public String userSearch(HttpServletRequest request,HttpServletResponse response) {
		String pageNum = request.getParameter("page");
		request.setAttribute("pageNum", pageNum);
		return "group/group";
	}
}
